using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;
    public float baseSpeed = 12f;
    public float gravity = -15f; // 增加重力，让下落更快
    public float jumpHeight = 3f;
    public float sprintSpeed = 5f;
    public float fallMultiplier = 2.5f; // 下落速度倍数
    
    [Header("人物模型")]
    public GameObject playerModel; // 人物模型对象
    public float jumpAnimationHeight = 0.5f; // 跳跃动画高度
    public float jumpAnimationSpeed = 10f; // 跳跃动画速度
    
    [Header("跳跃优化")]
    public float jumpCutMultiplier = 0.5f; // 松开跳跃键时的速度削减

    float speedBoost = 1f;
    Vector3 velocity;
    private bool isJumping = false;
    private float originalModelY;
    private float lastJumpTime = 0f;
    private float jumpCooldown = 0.1f; // 跳跃冷却时间
    void Start()
    {
        // 记录人物模型的原始Y位置
        if (playerModel != null)
        {
            originalModelY = playerModel.transform.localPosition.y;
        }
    }

    void Update()
    {
        // 简化的地面检测
        bool isGrounded = controller.isGrounded;
        
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (Input.GetButton("Fire3"))
            speedBoost = sprintSpeed;
        else
            speedBoost = 1f;

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * (baseSpeed + speedBoost) * Time.deltaTime);

        // 超灵敏的跳跃处理
        if (Input.GetButtonDown("Jump"))
        {
            // 极宽松的跳跃条件 - 几乎任何时候都能跳跃
            bool canJump = isGrounded || 
                          velocity.y <= 1.0f || 
                          !isJumping ||
                          controller.isGrounded ||
                          velocity.y <= 0.2f ||
                          (Time.time - lastJumpTime) > jumpCooldown;
            
            if (canJump)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
                isJumping = true;
                lastJumpTime = Time.time;
            }
        }
        
        // 连续跳跃检测 - 按住跳跃键也能跳跃
        if (Input.GetButton("Jump") && !isJumping && (Time.time - lastJumpTime) > jumpCooldown)
        {
            if (isGrounded || velocity.y <= 0.8f)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
                isJumping = true;
                lastJumpTime = Time.time;
            }
        }
        
        // 松开跳跃键时的速度削减
        if (Input.GetButtonUp("Jump") && velocity.y > 0)
        {
            velocity.y *= jumpCutMultiplier;
        }

        // 应用重力，下落时使用更快的重力
        if (velocity.y < 0)
        {
            velocity.y += gravity * fallMultiplier * Time.deltaTime;
        }
        else
        {
            velocity.y += gravity * Time.deltaTime;
        }

        controller.Move(velocity * Time.deltaTime);
        
        // 处理人物模型跳跃动画
        HandleJumpAnimation();
        
        // 重置跳跃状态 - 更宽松的重置条件
        if (isGrounded || velocity.y <= 0.3f)
        {
            isJumping = false;
        }
    }
    
    void HandleJumpAnimation()
    {
        if (playerModel == null) return;
        
        Vector3 modelPos = playerModel.transform.localPosition;
        
        if (isJumping)
        {
            // 更流畅的跳跃动画
            if (velocity.y > 0)
            {
                // 上升阶段 - 使用更平滑的曲线
                float jumpProgress = Mathf.Clamp01(velocity.y / Mathf.Sqrt(jumpHeight * -2f * gravity));
                float smoothProgress = Mathf.SmoothStep(0, 1, jumpProgress);
                modelPos.y = originalModelY + jumpAnimationHeight * smoothProgress;
            }
            else
            {
                // 下降阶段 - 快速回到原位
                modelPos.y = Mathf.Lerp(modelPos.y, originalModelY, Time.deltaTime * 15f);
            }
            
            playerModel.transform.localPosition = modelPos;
            
            // 如果落地了，停止跳跃动画
            if (controller.isGrounded && velocity.y <= 0.1f)
            {
                isJumping = false;
                modelPos.y = originalModelY;
                playerModel.transform.localPosition = modelPos;
            }
        }
        else
        {
            // 确保模型位置正确 - 使用更平滑的插值
            if (Mathf.Abs(modelPos.y - originalModelY) > 0.01f)
            {
                modelPos.y = Mathf.Lerp(modelPos.y, originalModelY, Time.deltaTime * 10f);
                playerModel.transform.localPosition = modelPos;
            }
        }
    }
}
